package edu.unc.comp410.colemangeer.demos;

import java.util.List;

import org.apache.log4j.Logger;
import org.omg.CORBA.portable.ApplicationException;

import edu.unc.comp410.colemangeer.data.ICustomerGateway;
import edu.unc.comp410.colemangeer.model.Catalog;
import edu.unc.comp410.colemangeer.model.CatalogBuilder;
import edu.unc.comp410.colemangeer.model.CatalogItem;
import edu.unc.comp410.colemangeer.model.CreditAccountAuthorization;
import edu.unc.comp410.colemangeer.model.CreditCardAuthorization;
import edu.unc.comp410.colemangeer.model.Customer;
import edu.unc.comp410.colemangeer.model.DataProvider;
import edu.unc.comp410.colemangeer.model.Order;
import edu.unc.comp410.colemangeer.model.PaymentAuthorization;
import edu.unc.comp410.colemangeer.model.Product;
import edu.unc.comp410.colemangeer.model.ShoppingCart;
import edu.unc.comp410.colemangeer.system.LoginService;

/**
 * 
 * will handle the browse and checkout use cases
 *
 */
public class ShoppingCartDemo {
	
	private static DataProvider dataProvider= new DataProvider();
	private static ICustomerGateway customerData;
	private static Catalog catalog;
	private static LoginService loginService;
	
	private static Customer customer;
	private static ShoppingCart cart;
	private static Order order;
	
	private static Logger log = Logger.getLogger(ShoppingCartDemo.class);
	
	
	public ShoppingCartDemo(){
		dataProvider = new DataProvider();
		if (!init()){
			throw new IllegalStateException();
		}
		cart = new ShoppingCart(customer);	
	}
	
	private boolean init(){
		try {
			customerData = dataProvider.getCustomerGateway();
		} catch (ApplicationException e) {
			System.err.println ("NO CUSTOMER GATEWAY!");
			return false;
		}
		
		try {
			catalog = new CatalogBuilder(dataProvider.getProductGateway()).getCatalog();
		} catch (ApplicationException e) {
			System.err.println ("NO PRODUCT GATEWAY!");
			return false;
		}
		
		loginService = new LoginService(customerData);
		
		//customer = customerData.getById(255); //Texas customer! Demonstrates tricky tax rule
		
		customer = loginService.logIn("Kye Hedlund", "password");
		
		if (customer == null){
			log.warn ("No customer! This demo is over.");
			return false;
		}
		
		log.info(customer.toString());
		return true;
	}
	public static void main(String[] args) {
		
		ShoppingCartDemo demo = new ShoppingCartDemo();
		CatalogItem itemOneToAdd, itemTwoToAdd;
		
		itemOneToAdd = catalog.getCatalogItemById(13);
		cart.addItem(itemOneToAdd, 2);
		
		itemTwoToAdd = catalog.getCatalogItemById(28);
		cart.addItem(itemTwoToAdd, 1); //has bundle and products
		
		// crude test of the tax rules
		
		log.warn("item one base price: " + itemOneToAdd.getSalePrice());
		log.warn("item one tax price: " + itemOneToAdd.getTaxedSalePrice(customer.createDefaultShippingInfo()));
		
		log.warn("item two base price: " + itemTwoToAdd.getSalePrice());
		log.warn("item two tax price: " +  itemTwoToAdd.getTaxedSalePrice(customer.createDefaultShippingInfo()));
		
		// crude test of CatalogItem.listProducts
		
		List<Product> itemTwoProducts = itemTwoToAdd.getAllProducts();
		log.warn("Products in catalog item 28:");
		for (Product prod: itemTwoProducts){
			log.warn (prod.getName());
		}
		
		//bad checkout
		try {
			order = cart.completeCheckout();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//prepare cart for checkout
		cart.setShippingInfo(customer.createDefaultShippingInfo());
		PaymentAuthorization creditCardAuth = new CreditCardAuthorization();
		PaymentAuthorization  accountAuth = new CreditAccountAuthorization();
		
		if  (accountAuth.authorize(customer)){
			cart.setPaymentAuthorization(accountAuth);
		}
		
		else if (creditCardAuth.authorize(customer)){
			cart.setPaymentAuthorization(creditCardAuth);
		}
		else{
			log.warn("Can't get payment auth!");
		}

		// good checkout
		try {
			order = cart.completeCheckout();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}

}
